########
# Usage: Rscript detail_longRNA_violin2.R 'TSPAN6' "TRUE" "wilcox.test" "Healthy,HCC"
#        Rscript detail_longRNA_violin2.R 'TSPAN6' "FALSE" "wilcox.test" "Healthy,HCC"
# 传入参数： target = 'TSPAN6'  
#            log = "TRUE"                    # log = "FALSE"   or   log = "TRUE"
#            testMethod = "wilcox.test"      # testMethod = "wilcox.test"   or   testMethod = "t.test"
#            cohort.selected = c('Healthy', 'HCC')
#
########
library(ggplot2)
library(ggpubr)
library(RColorBrewer)

library(optparse)

option_list <- list(
  make_option("--t", default = "", type = "character", help = "target"),
  make_option("--i", default = "", type = "character", help = "target file"),
  make_option("--l", default = "", type = "character", help = "log"),
  make_option("--m", default = "", type = "character", help = "test method"),
  make_option("--c", default = "", type = "character", help = "cohort selected"),
  make_option("--o", default = "", type = "character", help = "output png file")
)
opt <- parse_args(OptionParser(option_list = option_list))

Args = commandArgs()
target = opt$t
log = opt$l
testMethod = opt$m
cohort.selected = opt$c
cohort.selected = as.vector(unlist(strsplit(cohort.selected, ',')))

# target = 'TSPAN6'
# log = "FALSE"                   # log = "FALSE"   or   log = "TRUE"
# testMethod = "wilcox.test"
# cohort.selected = c('Healthy', 'HCC')
# path = "D:\\exo\\905\\submit 6.1\\detail_longRNA"
# setwd(path)

targetType = 'longRNA'
w=6
h=6
outfile = opt$o

infile =opt$i
data.target = read.csv(infile, sep=',', header = 1, row.names = NULL)
colnames(data.target) = c('sample', 'type', 'cohort', 'TPM')
data.target = data.target[data.target$cohort %in% cohort.selected, ]

cols = c(brewer.pal(9,"Pastel1"), brewer.pal(8,"Set3"))
my_comparisons <- list(cohort.selected)
if(log == "FALSE"){
  pdf(outfile,  width=w, height=h)
  p = ggplot(data.target, aes(x = cohort, y = TPM, fill=cohort))+
    geom_violin(alpha=1.1, width = 1, size=0.2, color="#424242") +
    geom_boxplot(width=0.1, fill="white", outlier.shape = NA, color="#1C1C1C", size=0.1)+
    stat_compare_means(comparisons = my_comparisons, method=testMethod,label = "p.signif", vjust = 0.5)+
    scale_fill_manual(values = cols)+
    xlab('')+ylab('TPM')+
    ggtitle(target)+
    theme_bw() + theme(panel.grid = element_blank())+
    theme(axis.title = element_text(size = 12, face = "bold"))+
    theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5))+
    guides(fill = FALSE, colour = FALSE)+
    theme(panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA),
          axis.text.y = element_text(face = 'bold',color = 'black',size = 10, angle = 360),
          axis.text.x = element_text(face = 'bold',color = 'black',size = 10, angle = 45, hjust = 1),
          axis.title = element_text(size = 14, face = "bold"))
}else{
  data.target$TPM = log2(data.target$TPM+1)
  pdf(outfile,  width=w, height=h)
  p = ggplot(data.target, aes(x = cohort, y = TPM, fill=cohort))+
    geom_violin(alpha=1.1, width = 1, size=0.2, color="#424242") +
    geom_boxplot(width=0.1, fill="white", outlier.shape = NA, color="#1C1C1C", size=0.1)+
    stat_compare_means(comparisons = my_comparisons, method=testMethod,label = "p.signif", vjust = 0.5)+
    scale_fill_manual(values = cols)+
    xlab('')+ylab('log2(TPM+1)')+
    ggtitle(target)+
    theme_bw() + theme(panel.grid = element_blank())+
    theme(axis.title = element_text(size = 12, face = "bold"))+
    theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5))+
    guides(fill = FALSE, colour = FALSE)+
    theme(panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA),
          axis.text.y = element_text(face = 'bold',color = 'black',size = 10, angle = 360),
          axis.text.x = element_text(face = 'bold',color = 'black',size = 10, angle = 45, hjust = 1),
          axis.title = element_text(size = 14, face = "bold"))
}
print(p)
dev.off()  





